import 'package:permission_handler/permission_handler.dart';

class PermissionUtil {
  //检查是否已有读写内存权限
  static Future<bool> checkStoragePermission() async {
    PermissionStatus status = await Permission.storage.status;
    //判断如果还没拥有读写权限就申请获取权限
    if (!status.isGranted) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.storage,
      ].request();

      if (statuses[Permission.storage] != null) {
        if (!statuses[Permission.storage]!.isGranted) {
          return false;
        }
      }
    }

    return true;
  }

  static Future<bool> checkLocationPermission() async {
    PermissionStatus status = await Permission.location.status;
    //判断如果还没就申请获取权限
    if (!status.isGranted) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.location,
      ].request();

      if (statuses[Permission.location] != null) {
        if (!statuses[Permission.location]!.isGranted) {
          return false;
        }
      }

      return true;
    }

    return true;
  }

  static Future<bool> checkPushPermission() async {
    PermissionStatus status = await Permission.notification.status;
    if (!status.isGranted) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.notification,
      ].request();

      if (statuses[Permission.notification] != null) {
        if (!statuses[Permission.notification]!.isGranted) {
          return false;
        }
      }

      return true;
    }

    return true;
  }
}